#!/bin/bash

#
# This script is called by extract_apks_paprellel.sh with an apk filename,
# and does several things:
# 1. The apk is extracted into a folder
# 2. The AndroidManifest.xml files are converted from binary to ASCII
# 3. The classes.dex file is converted to classes-dex2jar.jar
# 4. The classes in the jar file are printed to classes.list
# 5. The jar file is disassembled to disassembled.code
#
# Dependencies: unzip, java, javap, xmllint, dex2jar
#

if [ -z "$1" ]; then
    echo "Usage: extract_apk.sh /path/to/apk"
    exit 1
fi

apk="$1"
folder="${apk::-4}"
mkdir -p "$folder"
echo "Extracting $apk"
unzip -u -d "$folder" "$apk" 1>/dev/null
if [ $(ls -l "$folder" | wc -l) -gt 1 ]; then
  echo `basename $apk` >> valid_apks.txt
  cd "$folder"
  if [ -e "AndroidManifest.xml" ] && [ ! -e "AndroidManifest_ascii.xml" ]; then
    echo "Converting $folder/AndroidManifest.xml to ASCII"
    cp AndroidManifest.xml AndroidManifest_bin.xml
    java -jar ~/AXMLPrinter2.jar AndroidManifest_bin.xml > AndroidManifest_ascii.xml
    echo "Cleaning up $folder/AndroidManifest.xml"
    xmllint AndroidManifest_ascii.xml > AndroidManifest.xml
    if [ $? -ne 0 ]; then
      echo `basename $apk` >> ../malformed_xml.txt
    fi
  fi
  if [ -e "classes.dex" ] && [ ! -e "classes.list" ]; then
    echo "Converting $folder/classes.dex to jar format"
    d2j-dex2jar.sh classes.dex
    if [ $? -ne 0 ]; then
      echo `basename $apk` >> ../malformed_dex.txt
    else
      jar -tf classes-dex2jar.jar > classes.list
      javap -c -classpath classes-dex2jar.jar $(jar -tf classes-dex2jar.jar | grep "class$" | sed s/\.class$//) > disassembled.code
    fi
  fi
  cd ..
fi
